/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modelo;

import java.util.ArrayList;
import java.util.Vector;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Sergio
 */
public class TabelaConsulta extends AbstractTableModel{

    protected Vector dataVector;
    
    private ArrayList<Paciente> pacientes = new ArrayList<Paciente>();
    private String[] colunas = {"Código","Nome","RG"};
    
    public static final int INDICE_CODIGO = 0;
    public static final int INDICE_NOME = 1;
    public static final int INDICE_RG = 2;
    
    @Override
    public int getRowCount() {
        return pacientes.size();
    }

    @Override
    public int getColumnCount() {
        return colunas.length;
    }
    @Override
    public String getColumnName(int coluna) {
        return colunas[coluna];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        String[] linha = pacientes.get(rowIndex).toArray();
        return linha[columnIndex];
    }
    public void addPaciente(Paciente paciente) {
        pacientes.add(paciente);
        this.fireTableDataChanged();
    }

    public Paciente getSelectedPaciente(int linhaSelecionada) {
        return pacientes.get(linhaSelecionada);
    }
    
    public void removePaciente(Paciente paciente){
        pacientes.remove(paciente);
        this.fireTableDataChanged();
    }
    
    public void limpaArray(){
        pacientes.clear();
        this.fireTableDataChanged();
    }
    
    private void justifyRows(int from, int to) {
        // Sometimes the DefaultTableModel is subclassed
        // instead of the AbstractTableModel by mistake.
        // Set the number of rows for the case when getRowCount
        // is overridden.
        dataVector.setSize(getRowCount());

        for (int i = from; i < to; i++) {
            if (dataVector.elementAt(i) == null) {
                dataVector.setElementAt(new Vector(), i);
            }
            ((Vector)dataVector.elementAt(i)).setSize(getColumnCount());
        }
    }
    
    public void setNumRows(int rowCount){
        int old = getRowCount();
        if (old == rowCount) {
            return;
        }
        dataVector.setSize(rowCount);
        if (rowCount <= old) {
            fireTableRowsDeleted(rowCount, old-1);
        }
        else {
            justifyRows(old, rowCount);
            fireTableRowsInserted(old, rowCount-1);
        }
        
        limpaArray();
    }
    
}
